package com.yryp.sign.web.controller;

import com.alibaba.fastjson.JSON;
import com.yryp.sign.common.Result;
import com.yryp.sign.common.ResultGenerator;
import com.yryp.sign.mq.Sender;
import com.yryp.sign.utils.Base64Util;
import com.yryp.sign.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private Sender sender;

    @GetMapping("/send")
    @ResponseBody
    public Result<Integer> sendMessage(){
        Map<String, String> map = new HashMap<>();
        map.put("invitationCode", "B0015");
        map.put("oneself", "1");
        map.put("name", "丁文江");
        map.put("documentNumber", null);
        map.put("signTime", DateTimeUtil.getFormatCurrentTime());
        try {
            sender.send(JSON.toJSONString(map));
        } catch (Exception e) {
            //rsa error
            e.printStackTrace();
        }
        return ResultGenerator.genSuccessResult(0);
    }
    @GetMapping("/convert")
    @ResponseBody
    public Result<List<String>> convertImg(){
        List<String> list = new ArrayList<>();
        File file1 = null;
        File file2= null;
        File file3 = null;
        try {
            file1 = new ClassPathResource("static/img/bg.jpg").getFile();
            file2 = new ClassPathResource("static/img/logo.jpg").getFile();
            file3 = new ClassPathResource("static/img/title.png").getFile();
        } catch (IOException e) {
            e.printStackTrace();
            return ResultGenerator.genSuccessResult(new ArrayList<>());
        }
        list.add(Base64Util.geBase64Str(file1));
        list.add(Base64Util.geBase64Str(file2));
        list.add(Base64Util.geBase64Str(file3));
        return ResultGenerator.genSuccessResult(list);
    }

}